﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class admin_EditPengarang : Form
    {
        public admin_EditPengarang()
        {
            InitializeComponent();
        }

       // const string konek = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Haris\Documents\KULIAH\Semester 6\Framework .NET (E)\FP_DOTNET_dev\Perpus\Perpus\COBA.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True");

        NhibernateHelper help = new NhibernateHelper();
        //string konek = MappingDatabase.koneksi;

        //private ISessionFactory CreateSessionFactory()
        //{
        //    return Fluently.Configure()
        //        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(konek))
        //        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PengarangMap>())
        //        .BuildSessionFactory();
        //}

        private void button3_Click(object sender, EventArgs e)
        {
            TampilanBuku bookview = new TampilanBuku();
            bookview.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var writer = new Pengarang
                    {
                        // kode_buku = int.Parse(textBox1.Text),
                        nama_pengarang = textBox2.Text
                        //Name = txtCategoryName.Text,
                        //Description = txtCategoryDescription.Text
                    };

                    session.Save(writer);
                    transaction.Commit();

                    MessageBox.Show("Berhasil!");

                    //this.kategoriTableAdapter.Fill(this.cobaDataSet.Kategori);
                }
            } 
        }

        private void admin_EditPengarang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Pengarang' table. You can move, or remove it, as needed.
            this.pengarangTableAdapter.Fill(this.dotnetDataSet.Pengarang);
            // TODO: This line of code loads data into the 'cobaDataSet.Pengarang' table. You can move, or remove it, as needed.
           
            // TODO: This line of code loads data into the 'cobaDataSet.Penerbit' table. You can move, or remove it, as needed.
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            int id = Convert.ToInt32(textBox1.Text);

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var idPengarang = session.Get<Pengarang>(id);
                    idPengarang.nama_pengarang = textBox2.Text;

                    transaction.Commit();

                }
                MessageBox.Show("Berhasil!");
            }   
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;

            textBox1.Text = dataGridView1[0, i].Value.ToString();
            textBox2.Text = dataGridView1[1, i].Value.ToString();

            button1.Enabled = false;
        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";

            button1.Enabled = true;
        }
    }
}
